package pl.edu.utp.kursy.database.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import pl.edu.utp.kursy.database.CourseCategory;

public class CourseCategoryDao extends BaseDao<CourseCategory>{
    
    public CourseCategoryDao(SessionFactory sessionFactory){
        super(sessionFactory);
    }
    
    @Override
    public CourseCategory getById(int id) {
        
        Session session = sessionFactory.openSession();
        
        Criteria c = session.createCriteria(CourseCategory.class);
        c.add(Restrictions.eq("id", id));    		    		
        CourseCategory courseCategory = (CourseCategory)c.uniqueResult();
        session.close();
        
        return courseCategory;
   }

    @Override
    public List<CourseCategory> getAll() {
        ArrayList<CourseCategory> categories;
        
        Session session = sessionFactory.openSession();

        Criteria c = session.createCriteria(CourseCategory.class);
        c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        categories = (ArrayList<CourseCategory>)c.list();
        session.close();
        
        return categories; 
    }    
}
